require 'sqlexec'
require 'table'

class Tables 
  def initialize(sqlexec)
    @sqlexec = sqlexec
    @tbnames = gettbnames
    @tables = Hash.new
    @tbnames.each do |tbname|
      @tables[tbname] = Table.new(@sqlexec, tbname)
    end
  end
  private
  def gettbnames 
    query =  "sp_tables @table_owner = \'dbo\', @table_qualifier = \'#{@sqlexec.db}\';"
    data = @sqlexec.selquery query
    data.map{|x| x[2] if x[2] != "sysdiagrams"}.compact
  end
  public
  def to_a
    @tables
  end
  def [](tbname)
    @tables[tbname]
  end
  def to_s
    @tbnames.join(', ').to_s
  end
  def size
    @tbnames.size
  end
end
